%--------------------------------------------------------------------------
% Script to generate impulse responses
%--------------------------------------------------------------------------

%--------------------------------------------------------------------------
% Housekeeping
%--------------------------------------------------------------------------

clear; 
clc;
close all

set(0,'defaultTextInterpreter','latex');

% set specs 
xmin  = 0;
xmax  = 2.6;
xn    = 100;
xgrid = linspace(xmin, xmax, xn);

% set specs
nDataSel  = '1';
figDir    = [pwd, '/', 'Figures' ,'/Para', nDataSel ,'/'];
[~, ~, ~] = mkdir(figDir);

%%
%--------------------------------------------------------------------------
% Load population IRFs
%--------------------------------------------------------------------------

popDir    = [pwd, '/Data/Para', nDataSel ,'/'];
pop_YY_IRF = csvread( [popDir, 'IRF_TFP_K_c.csv'], 0, 0);

%%
%--------------------------------------------------------------------------
% Load IRFs from Agg VAR.
%--------------------------------------------------------------------------

% set specs
nVARSpec  = '3';
nMCMCSpec = '1';
nMod      = 'AggVAR';

n_drawsread = 200; % set how many posterior draws to consider

sNameAggVAR = ['AggVAR', nVARSpec, '_MCMC', nMCMCSpec];

% load IRFs
% sh_id = 1: TFP shock
irfDir = [pwd, '/', 'results' ,'/Para', nDataSel ,'/', sNameAggVAR, '/'];
YY_IRF = csvread( [irfDir, sNameAggVAR, '_IRF_YY_AggSh1_pmean.csv'], 1, 0); 
[H, n_all] = size(YY_IRF);
H = H-1;

n_agg   = 2;
n_cross = n_all-n_agg;
YY_IRF_uncertainty = zeros(H+1, n_all, n_drawsread);

for pp = 1:n_drawsread
    YY_IRF_uncertainty(:,:,pp)=csvread( [irfDir, sNameAggVAR, '_IRF_YY_AggSh1_', num2str(pp), '.csv'], 1, 0);
end

% first column is TFP
TFP_IRF_AggVAR = squeeze(YY_IRF_uncertainty(:,1,:));
% second column is capital
K_IRF_AggVAR = squeeze(YY_IRF_uncertainty(:,2,:));

% Extract quantiles
TFP_IRF_AggVAR_q10 = quantile(TFP_IRF_AggVAR,0.1,2);
TFP_IRF_AggVAR_q50 = quantile(TFP_IRF_AggVAR,0.5,2);
TFP_IRF_AggVAR_q90 = quantile(TFP_IRF_AggVAR,0.9,2);

K_IRF_AggVAR_q10 = quantile(K_IRF_AggVAR,0.1,2);
K_IRF_AggVAR_q50 = quantile(K_IRF_AggVAR,0.5,2);
K_IRF_AggVAR_q90 = quantile(K_IRF_AggVAR,0.9,2);

%%
%--------------------------------------------------------------------------
% Load IRFs from functional VAR.
%--------------------------------------------------------------------------

% set specs 
nfVARSpec = '3';
nModSpec  = '3';
nMCMCSpec = '1';
nMod      = 'SS';

n_drawsread = 200; % set how many posterior draws to consider

sNamefVAR = ['fVAR', nfVARSpec, '_', nMod , nModSpec, '_MCMC', nMCMCSpec];

% load IRFs
% sh_id = 1: TFP shock
irfDir = [pwd, '/', 'results' ,'/Para', nDataSel ,'/', sNamefVAR, '/'];
YY_IRF = csvread( [irfDir, sNamefVAR, '_IRF_YY_AggSh1_pmean.csv'], 1, 0); 
[H, n_all] = size(YY_IRF);
H = H-1;

n_agg   = 2;
n_cross = n_all-n_agg;
YY_IRF_uncertainty = zeros(H+1, n_all, n_drawsread);

for pp = 1:n_drawsread
    YY_IRF_uncertainty(:,:,pp)=csvread( [irfDir, sNamefVAR, '_IRF_YY_AggSh1_', num2str(pp), '.csv'], 1, 0);
end

% first column is TFP
TFP_IRF_fVAR = squeeze(YY_IRF_uncertainty(:,1,:));
% second column is capital
K_IRF_fVAR = squeeze(YY_IRF_uncertainty(:,2,:));

% Extract quantiles
TFP_IRF_fVAR_q10 = quantile(TFP_IRF_fVAR,0.1,2);
TFP_IRF_fVAR_q50 = quantile(TFP_IRF_fVAR,0.5,2);
TFP_IRF_fVAR_q90 = quantile(TFP_IRF_fVAR,0.9,2);

K_IRF_fVAR_q10 = quantile(K_IRF_fVAR,0.1,2);
K_IRF_fVAR_q50 = quantile(K_IRF_fVAR,0.5,2);
K_IRF_fVAR_q90 = quantile(K_IRF_fVAR,0.9,2);


%%
%--------------------------------------------------------------------------
% Figure: TFP IRF to a TFP shock
%--------------------------------------------------------------------------

hh = 59;

figure(1);clf;
set(figure(1),'PaperType','usletter','PaperOrientation','Landscape','PaperPosition',[0.1 0.1 11 8.5]);
plot(-1:hh,TFP_IRF_AggVAR_q10(1:hh+2,1),'Color','r','LineStyle',':','LineWidth',2)  
hold on
plot(-1:hh,TFP_IRF_AggVAR_q50(1:hh+2,1),'Color','r','LineStyle','-','LineWidth',4) 
hold on
plot(-1:hh,TFP_IRF_AggVAR_q90(1:hh+2,1),'Color','r','LineStyle',':','LineWidth',2) 
hold on
plot(-1:hh,TFP_IRF_fVAR_q10(1:hh+2,1),'Color','b','LineStyle',':','LineWidth',2)  
hold on
plot(-1:hh,TFP_IRF_fVAR_q50(1:hh+2,1),'Color','b','LineStyle','-','LineWidth',4) 
hold on
plot(-1:hh,TFP_IRF_fVAR_q90(1:hh+2,1),'Color','b','LineStyle',':','LineWidth',2) 
hold on
plot(-1:hh,pop_YY_IRF(1,1:hh+2)','Color','k','LineStyle','-','LineWidth',4) 
hold on
plot(-1:hh, zeros(size(-1:hh)), 'Color', 'k', 'LineStyle', '-', 'LineWidth',2)
set(gca,'FontSize',50)
xlim([0 hh])

sFigName = [sNamefVAR, '_', sNameAggVAR, '_IRF_AggSh1_TFP.pdf'];
saveas(figure(1), [figDir sFigName] );
% close all

%--------------------------------------------------------------------------
% Figure: K IRF to a TFP shock
%--------------------------------------------------------------------------

hh = 59;

figure(2);clf;
set(figure(2),'PaperType','usletter','PaperOrientation','Landscape','PaperPosition',[0.1 0.1 11 8.5]);
plot(-1:hh,K_IRF_AggVAR_q10(1:hh+2,1),'Color','r','LineStyle',':','LineWidth',2)  
hold on
plot(-1:hh,K_IRF_AggVAR_q50(1:hh+2,1),'Color','r','LineStyle','-','LineWidth',4) 
hold on
plot(-1:hh,K_IRF_AggVAR_q90(1:hh+2,1),'Color','r','LineStyle',':','LineWidth',2) 
hold on
plot(-1:hh,K_IRF_fVAR_q10(1:hh+2,1),'Color','b','LineStyle',':','LineWidth',2)  
hold on
plot(-1:hh,K_IRF_fVAR_q50(1:hh+2,1),'Color','b','LineStyle','-','LineWidth',4) 
hold on
plot(-1:hh,K_IRF_fVAR_q90(1:hh+2,1),'Color','b','LineStyle',':','LineWidth',2) 
hold on
plot(-1:hh,pop_YY_IRF(2,1:hh+2)','Color','k','LineStyle','-','LineWidth',4) 
hold on
plot(-1:hh, zeros(size(-1:hh)), 'Color', 'k', 'LineStyle', '-', 'LineWidth',2)
set(gca,'FontSize',50)
xlim([0 hh])
ylim([-0.15,0.25])
sFigName = [sNamefVAR, '_', sNameAggVAR, '_IRF_AggSh1_K.pdf'];
saveas(figure(2), [figDir sFigName] );
% close all



